package cn.fl.ass.service.impl;

import cmm.comm.facade.upload.api.IUploadApiService;
import cmm.comm.facade.upload.condition.SysUlDatCfgQueryCondition;
import cmm.comm.facade.upload.condition.SysUlFilesRecQueryCondition;
import cmm.comm.facade.upload.dto.SysUlDatCfgDTO;
import cmm.comm.facade.upload.dto.SysUlDatCfgDtlDTO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.ass.service.IAssDealCarService;
import cn.fl.ass.transform.AssDealCarVOTransform;
import cn.fl.ass.vo.AssDealCarVO;
import cn.fl.ass.vo.AssProFileVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.sue.vo.SueBscInfoVO;
import cn.fl.sue.vo.SueFileVO;
import cn.fl.system.service.ISysUlFilesRecService;
import cn.fl.system.service.ISysUsrService;
import cn.fl.system.vo.SysUlFilesRecVO;
import cn.fl.upload.constant.UploadConstant;
import fl.ass.facade.api.IAssDealCarApi;
import fl.ass.facade.condition.AssDealCarQueryCondition;
import fl.ass.facade.condition.AssProFileQC;
import fl.ass.facade.constant.AssFileConstant;
import fl.ass.facade.dto.AssDealCarDTO;
import fl.ass.facade.dto.AssTransferInfoDTO;
import fl.ass.facade.dto.MessageDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 处置-转优车信息表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class AssDealCarServiceImpl implements IAssDealCarService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(IAssDealCarService.class);
	
	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private IAssDealCarApi assDealCarApi;

	/**
	 * 上传文件的api接口服务
	 */
	@Autowired
	private IUploadApiService uploadApiService;

	/**
	 * 文件上传记录表 客户端服务层接口
	 */
	@Autowired
	private ISysUlFilesRecService sysUlFilesRecService;


	@Autowired
	private ISysUsrService usrService;

	@Override
	public AssDealCarVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			return null;
		}
		//TODO 业务逻辑
		MessageDTO<AssDealCarDTO> dto = this.assDealCarApi.findAssDealCarById(id);
		return AssDealCarVOTransform.toVO(dto.getData());
	}

	@Override
	public Long add(AssDealCarVO entity) throws BaseException{
		if(null == entity){
			return null;
		}
		//返回新增ID值-JL-20170331
		MessageDTO<String> response = this.assDealCarApi.saveAssDealCar(AssDealCarVOTransform.toDTO(entity));
		return response.getId();
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			return false;
		}
		//return this.assDealCarApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
	@Override
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(AssDealCarVO entity) throws BaseException{
		if(null == entity){
			return false;
		}
		MessageDTO<String> result = this.assDealCarApi.updateAssDealCar(AssDealCarVOTransform.toDTO(entity));
		return result.isSuccess();
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		return this.assDealCarApi.findAssDealCarListByPage(page);
	}

	@Override
	public List<AssDealCarVO> searchAssDealCarList(AssDealCarQueryCondition condition) throws BaseException {
		MessageDTO<AssDealCarDTO> dtoList = this.assDealCarApi.searchList(condition);
		List<AssDealCarVO> voList = new ArrayList<AssDealCarVO>();
		if(null != dtoList.getDatas()){
			for (AssDealCarDTO dto : dtoList.getDatas()) {
				voList.add(AssDealCarVOTransform.toVO(dto));
			}
		}
		return voList;
	}

	/**
	 * 获取维护过户方案文件的全部信息
	 *
	 * @param condition
	 * @return
	 * @throws BaseException
	 */
	@Override
	public List<AssProFileVO> searchAssProCfgDtlAndFileCfg(AssProFileQC condition) throws BaseException {
		return this.getAssProUploadFileList(condition);
	}

	/**
	 * 获取转优车资料的全部信息
	 *
	 * @param condition
	 * @return
	 * @throws BaseException
	 */
	private List<AssProFileVO> getAssProUploadFileList(AssProFileQC condition) throws BaseException {
		// 查询资料主配置：系统代码，组织架构代码，配置编号
		SysUlDatCfgQueryCondition ulDatCfgQueryCondition = new SysUlDatCfgQueryCondition();
		ulDatCfgQueryCondition.setOrgCd(condition.getOrgCd());
		ulDatCfgQueryCondition.setSysCd(CurrentThreadContext.getCurrentSysCd());//系统代码
		ulDatCfgQueryCondition.setCfgCdList(condition.getCfgCdList());//配置编号集合
		// 数据主键ID
		ulDatCfgQueryCondition.setDataId(String.valueOf(condition.getOptimalId()));
		List<SysUlDatCfgDTO> list = uploadApiService.searchSysUlDatCfgListByDataId(ulDatCfgQueryCondition);
		// 查询文件上传记录：数据主键
		SysUlFilesRecQueryCondition sysUlFilesRecQueryCondition = new SysUlFilesRecQueryCondition();
		if (StringUtils.isNotEmpty(condition.getOptimalId())) {
			sysUlFilesRecQueryCondition.setPkId(String.valueOf(condition.getOptimalId()));
		}
		sysUlFilesRecQueryCondition.setSysCd(AssFileConstant.ASS_PRO_SYS_CD);
		sysUlFilesRecQueryCondition.setIsDel(BaseConstant.IS_YESNO_NO);
		List<SysUlFilesRecVO> sysUlFilesRecVOS = sysUlFilesRecService.searchList(sysUlFilesRecQueryCondition);
		// 整合配置和文件上传记录到诉讼记录文件的VO
		List<AssProFileVO> voList = null;
		if (CollectionUtils.isNotEmpty(list)) {
			voList = new ArrayList<>();
			AssProFileVO vo;
			//设置路径
			String picServicePath = PropertiesCacheUtils.getProperty(UploadConstant.UPLOAD_SERVER_PATH);
			List<String> cfgCdDtlList = condition.getCfgCdDtlList();
			for (SysUlDatCfgDTO dto : list) {
				for (SysUlDatCfgDtlDTO dtlDTO : dto.getSysUlDatCfgDtlDTOList()) {
                    if (CollectionUtils.isNotEmpty(cfgCdDtlList)) {
                        for (String dtlCd : cfgCdDtlList) {
                            if (dtlDTO.getCfgDtlCd().equals(dtlCd)) {
                                vo = new AssProFileVO();
                                vo.setId(condition.getDataId());//业务主键
                                vo.setCfgDtlCd(dtlDTO.getCfgDtlCd());//配置子编号
                                vo.setDatNm(dtlDTO.getDatNm());//配置名称
                                vo.setCfgCd(dtlDTO.getCfgCd());//配置编号
                                vo.setOrgCd(dtlDTO.getOrgCd());//配置组织架构代码
                                vo.setCfgNm(dto.getCfgNm());//配置子名称
                                vo.setFileId(dtlDTO.getRecId());//附件ID
                                //如果有HTTP开头说明是可以访问路径,无需干涉
                                if (StringUtils.isNotBlank(dtlDTO.getFilePath()) && !dtlDTO.getFilePath().startsWith("http") && StringUtils.isNotBlank(picServicePath)) {
                                    vo.setFilePath(picServicePath + dtlDTO.getFilePath());
                                } else {
                                    vo.setFilePath(dtlDTO.getFilePath());
                                }
                                vo.setFileNm(dtlDTO.getRecFileNm());//上传名称
                                vo.setCrtTm(dtlDTO.getRecCrtTm());//上传时间
                                vo.setIsMust(dtlDTO.getIsMust());//是否必传
                                for (SysUlFilesRecVO sysUlFilesRecVO : sysUlFilesRecVOS) {
                                    if (StringUtils.isNotEmpty(sysUlFilesRecVO.getCrtUsrId())) {
                                        if (sysUlFilesRecVO.getCfgDtlCd().equals(dtlDTO.getCfgDtlCd())) {
                                            vo.setUpUsrNm(usrService.selectById(sysUlFilesRecVO.getCrtUsrId(), AssFileConstant.ASS_PRO_SYS_CD).getRefRlNm());
                                        }
                                    }
                                }
                                voList.add(vo);
                            }
                        }
                    } else {
                        vo = new AssProFileVO();
                        vo.setId(condition.getDataId());//业务主键
                        vo.setCfgDtlCd(dtlDTO.getCfgDtlCd());//配置子编号
                        vo.setDatNm(dtlDTO.getDatNm());//配置名称
                        vo.setCfgCd(dtlDTO.getCfgCd());//配置编号
                        vo.setOrgCd(dtlDTO.getOrgCd());//配置组织架构代码
                        vo.setCfgNm(dto.getCfgNm());//配置子名称
                        vo.setFileId(dtlDTO.getRecId());//附件ID
                        //如果有HTTP开头说明是可以访问路径,无需干涉
                        if (StringUtils.isNotBlank(dtlDTO.getFilePath()) && !dtlDTO.getFilePath().startsWith("http") && StringUtils.isNotBlank(picServicePath)) {
                            vo.setFilePath(picServicePath + dtlDTO.getFilePath());
                        } else {
                            vo.setFilePath(dtlDTO.getFilePath());
                        }
                        vo.setFileNm(dtlDTO.getRecFileNm());//上传名称
                        vo.setCrtTm(dtlDTO.getRecCrtTm());//上传时间
                        vo.setIsMust(dtlDTO.getIsMust());//是否必传
                        for (SysUlFilesRecVO sysUlFilesRecVO : sysUlFilesRecVOS) {
                            if (StringUtils.isNotEmpty(sysUlFilesRecVO.getCrtUsrRlNm())) {
                                if (sysUlFilesRecVO.getCfgDtlCd().equals(dtlDTO.getCfgDtlCd())) {
                                    vo.setUpUsrNm(sysUlFilesRecVO.getCrtUsrRlNm());
                                }
                            }
                        }
                        voList.add(vo);
                    }
                }
			}
		}
		return voList;
	}

	@Override
	public Pager searchZycListPage(Pager page) throws BaseException {
		if (null == page) page = new Pager();
		return assDealCarApi.searchUnionListByPage(page);
	}

    @Override
    public Pager searchAssTransferInfoList(Pager page) throws BaseException {
        if (null == page) {
            page = new Pager();
        }
        return assDealCarApi.searchAssTransferInfoList(page);
    }

    /**
     * 通过配置编号查询诉讼文件的配置信息
     *
     * @param condition
     * @return
     */
    @Override
    public List<AssProFileVO> searchAssTransferInfoFileCfgDtlAndFileCfg(AssProFileQC condition) {
        return this.getAssTransferInfoUploadFileList(condition);
    }

    /**
     * 获取诉讼记录文件的全部信息
     *
     * @param condition
     * @return
     * @throws BaseException
     */
    private List<AssProFileVO> getAssTransferInfoUploadFileList(AssProFileQC condition) throws BaseException {
        // 查询资料主配置：系统代码，组织架构代码，配置编号
        SysUlDatCfgQueryCondition ulDatCfgQueryCondition = new SysUlDatCfgQueryCondition();
        ulDatCfgQueryCondition.setOrgCd(condition.getOrgCd());
        ulDatCfgQueryCondition.setSysCd(CurrentThreadContext.getCurrentSysCd());//系统代码
        ulDatCfgQueryCondition.setCfgCdList(condition.getCfgCdList());//配置编号集合
        // 数据主键ID
        ulDatCfgQueryCondition.setDataId(String.valueOf(condition.getAssTransferInfoId()));
        List<SysUlDatCfgDTO> list = uploadApiService.searchSysUlDatCfgListByDataId(ulDatCfgQueryCondition);
        // 查询文件上传记录：数据主键
        SysUlFilesRecQueryCondition sysUlFilesRecQueryCondition = new SysUlFilesRecQueryCondition();
        if (StringUtils.isNotEmpty(condition.getAssTransferInfoId())) {
            sysUlFilesRecQueryCondition.setPkId(String.valueOf(condition.getAssTransferInfoId()));
        }
        // 整合配置和文件上传记录到诉讼记录文件的VO
        List<AssProFileVO> voList = null;
        if (CollectionUtils.isNotEmpty(list)) {
            voList = new ArrayList<>();
            AssProFileVO vo;
            //设置路径
            String picServicePath = PropertiesCacheUtils.getProperty(UploadConstant.UPLOAD_SERVER_PATH);
            for (SysUlDatCfgDTO dto : list) {
                for (SysUlDatCfgDtlDTO dtlDTO : dto.getSysUlDatCfgDtlDTOList()) {
                    vo = new AssProFileVO();
                    vo.setId(condition.getDataId());//业务主键
                    vo.setCfgDtlCd(dtlDTO.getCfgDtlCd());//配置子编号
                    vo.setDatNm(dtlDTO.getDatNm());//配置名称
                    vo.setCfgCd(dtlDTO.getCfgCd());//配置编号
                    vo.setOrgCd(dtlDTO.getOrgCd());//配置组织架构代码
                    vo.setCfgNm(dto.getCfgNm());//配置子名称
                    vo.setFileId(dtlDTO.getRecId());//附件ID
                    //如果有HTTP开头说明是可以访问路径,无需干涉
                    if (StringUtils.isNotBlank(dtlDTO.getFilePath()) && !dtlDTO.getFilePath().startsWith("http") && StringUtils.isNotBlank(picServicePath)) {
                        vo.setFilePath(picServicePath + dtlDTO.getFilePath());
                    } else {
                        vo.setFilePath(dtlDTO.getFilePath());
                    }
                    vo.setFileNm(dtlDTO.getRecFileNm());//上传名称
                    vo.setCrtTm(dtlDTO.getRecCrtTm());//上传时间
                    vo.setIsMust(dtlDTO.getIsMust());//是否必传
                    voList.add(vo);
                }
            }
        }
        return voList;
    }

    @Override
    public Long addAssTransferInfo(AssTransferInfoDTO dto) throws BaseException {
        return this.assDealCarApi.addAssTransferInfo(dto);
    }

    @Override
    public int modify4Null(AssDealCarVO entity) throws BaseException {
        return this.assDealCarApi.modify4Null(AssDealCarVOTransform.toDTO(entity));
    }
}

